package cn.lili.controller.test;


import cn.hutool.core.map.MapUtil;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.client.GoodsIndexClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dos.EsSupplierGoodsIndex;
import cn.lili.modules.search.entity.dto.EsDeleteDTO;
import cn.lili.modules.search.service.EsGoodsIndexService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;


/**
 * 此类提供的方法用于手动刷新es的数据
 */

@RestController
@RequestMapping("/goods/index/test")
@RequiredArgsConstructor
public class EsGoodsIndexTestController {

    private final EsGoodsIndexService goodsIndexService;

    private final GoodsService goodsService;

    /**
     * 更新店铺商品的es
     * @param storeId
     * @return
     */
    @GetMapping(value = "/store")
    public ResultMessage<String> updateStoreGoodsIndex(String storeId) {
        goodsIndexService.updateGoodsIndexByStore(storeId);
        return ResultUtil.success();
    }


    /**
     * 更新单个商品的es
     * @param goodsId
     * @return
     */
    @GetMapping()
    public ResultMessage<String> updateGoodsIndex(String goodsId) {
        Goods goods = goodsService.getById(goodsId);
        if(null!=goods){
            goodsIndexService.updateGoodsIndexByGoodsList(Collections.singletonList(goods));
        }else{
            //商品不存在，直接从ES中删除
            EsDeleteDTO deleteDTO = EsDeleteDTO.builder()
                    .queryFields(MapUtil.builder(new HashMap<String, Object>()).put("goodsId", goodsId).build())
                    .clazz(EsGoodsIndex.class)
                    .build();
            goodsIndexService.deleteIndex(deleteDTO);

            EsDeleteDTO deleteDTO1 = EsDeleteDTO.builder()
                    .queryFields(MapUtil.builder(new HashMap<String, Object>()).put("goodsId", goodsId).build())
                    .clazz(EsSupplierGoodsIndex.class)
                    .build();
            goodsIndexService.deleteIndex(deleteDTO1);
        }
        return ResultUtil.success();
    }
}
